import { isDate, isObject } from './util'

function encode(val: string): string {
  return encodeURIComponent(val)
    .replace(/%40/g, '@')
    .replace(/%3A/gi, ':')
    .replace(/%24/g, '$')
    .replace(/%2C/gi, ',')
    .replace(/%20/g, '+')
    .replace(/%5B/gi, '[')
    .replace(/%5D/gi, ']')
}


export function buildURL(url: string, params?: any): string {
  if (!params) {
    return url
  }
  const parts: string[] = []
  Object.keys(params).forEach(
    key => {
      const val = params[key]
      if (val == null || typeof val === 'undefined') {
        return
      }
      let valuse = []
      if (Array.isArray(val)) {
        valuse = val
        key += '[]'
      } else {
        valuse = [val]
      }
      valuse.forEach(
        val => {
          if (isDate(val)) {
            val = val.toISOString()
          } else if (isObject(val)) {
            val = JSON.stringify(val)
          }

          parts.push(`${key}=${val}`)
        }
      )


    }
  )
  return ''


}
